package game

type Food struct {
	Position *Point
}

type Snake struct {
	id string
	direction Direction
	// 0为头
	body []*Point
}

func NewSnake(id string, winSizer winSizer) *Snake {
	return &Snake{
		id: id,
		body: []*Point{ randAPosition(winSizer.Width(), winSizer.Height()) },
	}
}

func (snake *Snake) ID() string {
	return snake.id
}

func (snake *Snake) Frame() []*Point {
	return snake.body
}

func (snake *Snake) ChangeDirection(direction Direction) {
	if !canChangeDirection(snake.direction, direction) {
		return
	}
	snake.direction = direction
}

func (snake *Snake) Move(foods []*Food) (ateFoods []*Food) {
	ateFood := false
	ateFoods = foods
	moveTo := snake.body[0].Move(snake.direction)

	//log.L.Debug("move to", zap.Any("target", moveTo), zap.Any("foods", foods))
	for i, f := range foods {
		if moveTo.Equal(f.Position) {
			ateFoods = append(foods[:i], foods[i + 1:]...)
			ateFood = true
			break
		}
	}

	// don't drop last if eat food
	if !ateFood {
		snake.body = snake.body[:len(snake.body) - 1]
	}
	snake.body = append([]*Point{ moveTo }, snake.body...)

	return
}



